package com.wxxymakers.grademark.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wxxymakers.grademark.model.entity.WxDuty;
import com.wxxymakers.grademark.model.mapper.WxDutyMapper;
import com.wxxymakers.grademark.utils.NumUtils;
import com.wxxymakers.grademark.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @AUTHOR soft
 * @DATE 2018/12/21 15:06
 * @DESCRIBE 职务
 */
@Service
public class WxDutyService {
    
    @Autowired
    private WxDutyMapper dutyMapper;

    @Transactional
    public boolean add(WxDuty duty) {
        duty.setDatetime(new Date());
        return dutyMapper.insert(duty) > 0;
    }

    @Transactional
    public boolean delete(int id) {
        return dutyMapper.deleteById(id) > 0;
    }

    @Transactional
    public boolean change(WxDuty duty) {
        if (NumUtils.gtZero(duty.getId())) {
            return dutyMapper.updateById(duty) > 0;
        }
        return false;
    }

    public WxDuty byId(int id) {
        return dutyMapper.selectById(id);
    }

    public List<WxDuty> list() {
        return dutyMapper.selectList(null);
    }

    public Page<WxDuty> page(int page, int limit, WxDuty query) {
        QueryWrapper<WxDuty> wrapper = new QueryWrapper<>(query);
        wrapper.orderByDesc("id");

        long total = dutyMapper.selectCount(wrapper);
        if (total > 0) {
            List<WxDuty> wxDuties = dutyMapper.selectPage(PageUtils.rowBounds(page, limit), wrapper)
                    .getRecords();
            return PageUtils.page(wxDuties, page, limit, total);
        }
        return null;
    }
}
